package com.yc.baseapi;

import java.util.*;
import java.util.stream.Collectors;

public class TestJdk8 {

    public void testMap() {
        Map<String, String> items = new HashMap<>();
        items.put("A", "10");
        items.put("B", "20");
        items.put("C", "30");
        items.put("D", "40");
        items.put("E", "50");
        items.put("F", "60");

        items.forEach((k,v)->{
            System.out.println("Item : " + k + " Count : " + v);
            if("30".equals(v)){
                items.remove(k);//报错：java.util.ConcurrentModificationException
            }
        });
    }

    /**
     * 删除map中的元素的安全方法
     */
    public void testMap2() {
        Map<String, String> items = new HashMap<>();
        items.put("A", "10");
        items.put("B", "20");
        items.put("C", "30");
        items.put("D", "40");
        items.put("E", "50");
        items.put("F", "60");

        String delKey = null;
        for (Map.Entry<String, String> entry: items.entrySet()) {
            if ("30".equals(entry.getValue())) {
                delKey = entry.getKey();
                break;
            }
        }
        items.remove(delKey);

        items.forEach((k,v)->{
            System.out.println("Item : " + k + " Count : " + v);
        });
    }
    
    /**
     * 使用流拼接集合
     */
    public void m1() {
    	List<String> a = Arrays.asList("a","b","c");
    	System.out.println(a.stream().collect(Collectors.joining(":")));
    	
    }


    public void testList() {
        List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(4);

        List<Integer> b = new ArrayList<>();
        b.add(5);
        b.add(6);
        b.add(3);
        b.add(4);

        a.retainAll(b);
        System.out.println(a);


    }
    

    public static void main(String[] args) {
        TestJdk8 tj = new TestJdk8();
//        tj.testMap();
//        tj.testMap2();
//        tj.m1();
        tj.testList();
    }
}
